package utils;

import android.graphics.Bitmap;
import android.util.LruCache;

/**
 *   内存缓存工具类
 * Author：Wbin
 * E_mail：atwbin@163.com
 */

public class MomeryCacheUtils {
    private LruCache<String,Bitmap> mlruCache;

    public MomeryCacheUtils(){
        //得到当前应用运行时的内存
        long maxMemory = Runtime.getRuntime().maxMemory();
        long maxSize = maxMemory/8;
        mlruCache = new LruCache<String,Bitmap>((int)maxSize){
            @Override
            protected int sizeOf(String key, Bitmap value) {
                return value.getRowBytes() * value.getHeight();
            }
        };
    }

    /**
     * 从内从中获取图片
     * @param url
     * @return
     */
    public Bitmap getBitmapFromUrl(String url){
        return mlruCache.get(url);
    }

    /**
     * 根据url将图片保存到内存中
     * @param url
     * @param bitmap
     */
    public void putBitmap2Momery(String url,Bitmap bitmap){
        mlruCache.put(url,bitmap);
    }
}
